package org.nlp2rdf.navigator.client.functionality;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TitleWidget extends VerticalPanel {

	public static String SHOW = "show";
	public static String HIDE = "hide";
	public static String STYLE = "module-title";

	final private HorizontalPanel top = new HorizontalPanel();
	final private HorizontalPanel bottom = new HorizontalPanel();
	final private Label titelLabel;
	final private Label collapseLabel;
	final private List<Panel> targetPanels;

	/**
	 * calls disableHideShowFeature();
	 * @param title
	 */
	public TitleWidget(String title) {
		this(title, (Panel) null);
		disableHideShowFeature();
	}
	
	public TitleWidget(String title, Panel targetPanel) {
		this(title, new ArrayList<Panel>(Arrays.asList(new Panel[] { targetPanel })));
	}
	

	public TitleWidget(String title, Panel... targetPanels) {
		this(title, Arrays.asList(targetPanels));
	}

	public TitleWidget(String title, List<Panel> targetPanels) {
		this.titelLabel = new Label(title);
		titelLabel.addStyleName(STYLE);
		

		this.targetPanels = targetPanels;

		this.collapseLabel = new Label(SHOW);
		collapseLabel.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent arg0) {
				if (collapseLabel.getText().equals(SHOW)) {
					collapse(false);
				} else {
					collapse(true);
				}

			}
		});

		top.add(titelLabel);
		top.setWidth("100%");
		

		bottom.setVerticalAlignment(ALIGN_MIDDLE);
		bottom.add(collapseLabel);
		bottom.setWidth("100%");
//		bottom.setCellVerticalAlignment(collapseLabel, HasVerticalAlignment.ALIGN_MIDDLE);

		this.add(top);
		this.add(bottom);
		this.setStyleName(STYLE);
		this.setWidth("100%");

	}
	
	public void disableHideShowFeature(){
		bottom.remove(collapseLabel);
	}

	/**
	 * 
	 * @param collapse true for hidden, false for shown
	 */
	public void collapse(boolean collapse) {
		if (collapse) {
			for (Panel p : targetPanels) {
				p.setVisible(false);
			}
			collapseLabel.setText(SHOW);
		} else {
			for (Panel p : targetPanels) {
				p.setVisible(true);
			}
			collapseLabel.setText(HIDE);
		}

	}
	
	public void hide(){
		collapse(true);
	}
	public void show(){
		collapse(false);
	}

	public HorizontalPanel getTop() {
		return top;
	}

	public HorizontalPanel getBottom() {
		return bottom;
	}

	public Label getTitelLabel() {
		return titelLabel;
	}

	public Label getCollapseLabel() {
		return collapseLabel;
	}

	public List<Panel> getTargetPanels() {
		return targetPanels;
	}

}
